package com.ty.zls.listener;

import android.content.Context;

import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.ty.zls.adapter.NeedWaterAdapter;
import com.ty.zls.task.NeedWaterTask;
import com.ty.zls.util.StrogeUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 抄表刷新listener
 */
public class NeedWaterRefreshListener implements OnRefreshListener {

    private NeedWaterAdapter adapter;
    private Context context;
    private SwipeToLoadLayout layout;
    private Map<String, String> request = new HashMap<>();
    private QMUIEmptyView emptyView;

    public NeedWaterRefreshListener(NeedWaterAdapter adapter, Context context, SwipeToLoadLayout swipeToLoadLayout, QMUIEmptyView qmuiEmptyView) {
        this.adapter = adapter;
        this.context = context;
        this.layout = swipeToLoadLayout;
        this.emptyView = qmuiEmptyView;
    }

    @Override
    public void onRefresh() {
        layout.setRefreshing(false);
        String token = StrogeUtil.getStringFraomPs(context, StrogeUtil.user, StrogeUtil.token);
        request.put("token", token);
        adapter.setPageNum(1);
        adapter.setTotal(0);
        adapter.getDatas().clear();
        request.put("pageNum", String.valueOf(1));
        request.put("pageSize", String.valueOf(adapter.getPageSize()));
        new NeedWaterTask(emptyView,context,adapter,layout).execute(request);
    }

    public void autoRefresh() {
        layout.post(new Runnable() {
            @Override
            public void run() {
                layout.setRefreshing(true);
            }
        });
    }
}
